#include "byte_stream.hh"

using namespace std;

ByteStream::ByteStream( uint64_t capacity ) : capacity_( capacity ) {}

bool Writer::is_closed() const
{
  // Your code here.
  return is_closed_;
}

void Writer::push( string data )
{
  // Your code here.
  uint64_t len = data.length();
  len = min( len, capacity_ - bytes_pushed_ + bytes_popped_ );
  data = data.substr( 0, len );
  bytes_pushed_+=len;
  buff_.append( data );
}

void Writer::close()
{
  // Your code here.
  is_closed_ = true;
}

uint64_t Writer::available_capacity() const
{
  // Your code here.
  return capacity_ - bytes_pushed_ + bytes_popped_;
}

uint64_t Writer::bytes_pushed() const
{
  // Your code here.
  return bytes_pushed_;
}

bool Reader::is_finished() const
{
  // Your code here.
  return is_closed_ && ( bytes_pushed_ - bytes_popped_ == 0 );
}

uint64_t Reader::bytes_popped() const
{
  // Your code here.
  return bytes_popped_;
}

string_view Reader::peek() const
{
  // Your code here.
  return { this->buff_ };
}

void Reader::pop( uint64_t len )
{
  // Your code here.
  if ( len > buff_.length() )
    return;
  buff_.erase( 0, len );
  bytes_popped_+=len;
}

uint64_t Reader::bytes_buffered() const
{
  // Your code here.
  return bytes_pushed_ - bytes_popped_;
}
